#!/bin/bash
function install_apache() {
    echo "安装Apache服务" >>$logs_file
    rm -rf /tmp/.apache.err.info
    apachePid=`pidof httpd`
    if [ -n "$apachePid" ];then
        whiptail --backtitle "Author: Elvis" --title "警告" --msgbox "系统已有正在运行中的Apache，请删除后继续！！！" 10 70
        sleep 0.15
        return 1
    fi
    
    apacheVer=$(whiptail --backtitle "Author: Elvis" --title "Apache安装配置" --inputbox \
        "输入需要安装的Apache版本，如果本地不存在，则需要联网下载" 10 60 2.4.43 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    
    if [ -z "$apacheVer" ];then
        apacheVer="2.4.43"
    fi
    apacheVer=`echo $apacheVer`    
    
    if [ ! -f $workHome/packages/apache/httpd-$apacheVer.tar.bz2 -a "$die_network" == "ture" ];then
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "系统未联网，且本地不存在需要安装的Apache版本！！！" 10 70
        sleep 0.10
        return 1
    fi
    apachePath=$(whiptail --backtitle "Author: Elvis" --title "Apache安装配置" --inputbox "输入Apache安装位置" 10 60 "/usr/local/apache2" 3>&1 1>&2 2>&3)
    if [ "$?" != "0" ];then
        sleep 0.20
        return 1
    fi
    apachePath=${apachePath:-/usr/local/apache2}
    apachePath=`echo ${apachePath}`
    apachePath=${apachePath%/}
    isNull=`ls $apachePath/* 2>/dev/null | wc -l`
    if [[ $isNull -gt 0 ]];then
        whiptail --backtitle "Author: Elvis" --title "警告" --yes-button "继续" --no-button "退出" --yesno "给出的Apache安装目录非空，继续安装会删除已有数据！\n\n是否继续安装？" 10 60
        if [ "$?" != "0" ];then
         sleep 0.10
         return 1
        fi
    fi
    command -v ss >/dev/null 2>&1
    if [ "$?" == "0" ];then
        listen_cmd="ss -lnpt"
    else
        command -v netstat >/dev/null 2>&1
        if [ "$?" == "0" ];then
          listen_cmd="netstat -tnlp"
        fi
    fi
    [[ $listen_cmd ]] && PortCheck=Yes || PortCheck=No
    PortUsed=
    PortUsed_Pro=
    apachePort=
    while [[ ! "$apachePort" =~ ^[1-9][0-9]+$ || "$PortUsed" == "Yes" ]]
    do
        apachePort=$(whiptail --backtitle "Author: Elvis" --title "Nexus配置" --inputbox --nocancel \
        "请输入Aapche监听端口，注意端口是否被占用" 10 60 "80" 3>&1 1>&2 2>&3)
        if [[ ! "$apachePort" =~ ^[1-9][0-9]+$ ]];then
             whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Apache端口只能为两位及以上且不以0开头的数字组成，请重新输入！" 10 60
             sleep 0.5
        else
            if [ "$PortCheck" == "Yes" ];then
                PortUsed_Pro=`$listen_cmd 2>/dev/null |grep ":${apachePort} "`
                if [ -n "$PortUsed_Pro" ];then
                  PortUsed=Yes
                  whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "Apache端口已被使用，请重新输入！" 8 60
                else
                  PortUsed=No
                fi
            else
                PortUsed=No
            fi
        fi
    done
    apachePort=${apachePort:-80}
    apachePort=`echo $apachePort`

    whiptail --backtitle "Author: Elvis" --title "Apache安装信息汇总" --yesno "\nApache版本：$apacheVer\nApache安装位置：$apachePath\nApache端口：$apachePort\n\nYes以继续安装，No退出Apache安装" 15 70
    if [ "$?" != "0" ];then
        sleep 0.10
        return 1
    fi
    
    apacheHome="$workHome/packages/apache"
    cd $apacheHome
    {
    echo 1
    if [ "$die_network" == "true" ];then
        ping -c 2 -w 5 180.76.76.76 >/dev/null 2>&1
        if [ "$?" != "0" ];then
           die_network="true"
        else
           die_network="false"
        fi
    else
        sleep 1
    fi
    if [[ ! -f ${apacheHome}/httpd-${apacheVer}.tar.bz2 && "$die_network" == "false" ]];then
        msg="下载apache安装包"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n3\n$msg\nXXX"
        echo 3
        echo "首先使用curl下载，curl失败后尝试使用wget下载" >>$logs_file
        echo -e "XXX\n5\n下载apache安装包中......\nXXX"
        echo 5
        curl --connect-timeout 30 -m 360 --fail -O https://mirrors.huaweicloud.com/apache/httpd/httpd-$apacheVer.tar.bz2 >>$logs_file 2>&1
        if [ "$?" != "0" ];then
            rm -rf ${apacheHome}/httpd-${apacheVer}.tar.bz2
            echo "">>$logs_file
            curl --connect-timeout 30 --fail -O https://archive.apache.org/dist/httpd/httpd-$apacheVer.tar.bz2 >>$logs_file 2>&1
        fi
                
        if [ "$?" != "0" ];then
           msg="下载apache失败，请检查网络或者不存在此版本的Apache！！！"
           echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
           echo -e "XXX\n8\n$msg\nXXX"
           echo 8
           echo "$msg" >/tmp/.apache.err.info
           sleep 3
           return 1
        fi
    elif [[ ! -f ${apacheHome}/httpd-${apacheVer}.tar.bz2 && "$die_network" == "true" ]];then
        msg="你的主机未联网，请提前手动下载安装包，使用离线安装！！！"
        echo "$msg" >/tmp/.apache.err.info
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n8\n$msg\nXXX"
        echo 8
        sleep 3
        return 1
    fi
    
    
    msg="安装所需基础RPM包"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n10\n$msg\nXXX"
    echo 10
    yum -y install make gcc gcc-c++ aprutil aprutil-devel openldap openldap-devel apr apr-util-ldap lynx mailcap apr-devel expat-devel bzip2 libtool libtool-ltdl libtool-ltdl-devel libcurl libcurl-devel jansson jansson-devel nghttp2 nss nss-devel openssl openssl-devl glibc glibc-devel automake>>$logs_file 2>&1
    if [ "$?" != "0" ];then
        msg="基础依赖软件失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n15\n$msg\nXXX"
        echo 15
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    
    for i in make gcc gcc-c++ openssl openssl-devel libcurl libcurl-devel apr apr-devel libtool glibc automake
    do
        rpm -q $i >/dev/null 2>&1
        if [ "$?" != "0" ];then
            msg="基础依赖软件${i}安装失败，退出安装！！！"
            echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
            echo -e "XXX\n15\n$msg\nXXX"
            echo 15
            echo "$msg" >/tmp/.apache.err.info
            sleep 3
            return 1
        fi
    done
    
    find $apacheHome/* -type d|xargs rm -rf
    echo "注意：以下编译安装过程，仅输出错误信息，编译日志存放在/tmp/apache-comp.log" >>$logs_file
    msg="安装apr"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n20\n$msg\nXXX"
    echo 20
    echo "$msg" >/tmp/apache-comp.log
    cd $apacheHome
    aprPack=`ls -r |grep apr-[0-9]|head -n 1`
    if [ -z "$aprPack" ];then
        msg="下载apr"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n22\n$msg\nXXX"
        echo 22
        curl --connect-timeout 30 -m 360 --fail -O https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.bz2 >>$logs_file 2>&1
        download_status=$?
    fi
    if [ "${download_status}" != "0" ];then
        rm -rf apr-1.7.0.tar.bz2
        curl --connect-timeout 30 -m 360 --fail -LkO https://gitee.com/ElvisTan/software-house/attach_files/974551/download/apr-1.7.0.tar.bz2 >>$logs_file 2>&1
        download_status=$?
    fi
    
    if [ "${download_status}" != "0" ];then
        msg="apr下载失败，退出安装！！！"
        rm -rf apr-1.7.0.tar.bz2
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n25\n$msg\nXXX"
        echo 25
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    
    aprPack=`ls -r |grep apr-[0-9]|head -n 1`
    apruPack=${aprPack%.tar*}
    echo $aprPack|grep ".tar.gz">/dev/null && tar zxf $aprPack >/dev/null 2>>$logs_file ||tar jxf $aprPack >/dev/null 2>>$logs_file
    cd $apruPack && echo -e "XXX\n21\n预编译apr中......\nXXX" && echo 21 && ./configure --prefix=/usr/local/apr >>/tmp/apache-comp.log 2>>$logs_file && \
    echo -e "XXX\n22\n编译apr中......\nXXX" && echo 22 && make -j8 >>/tmp/apache-comp.log 2>>$logs_file && \
    echo -e "XXX\n24\n安装apr中......\nXXX" && echo 24 && make install >>/tmp/apache-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装apr失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n25\n$msg\nXXX"
        echo 25
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/apache-comp.log
    
    msg="安装aprutil"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n30\n$msg\nXXX"
    echo 30
    echo "$msg">>/tmp/apache-comp.log
    cd $apacheHome
    aprutilPack=`ls -r |grep apr-util|head -n 1`
    if [ -z "$aprutilPack" ];then
        msg="下载apr-util"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n32\n$msg\nXXX"
        echo 32
        curl --connect-timeout 30 -m 360 --fail -O https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.bz2 >>$logs_file 2>&1
        download_status=$?
    fi
    if [ "${download_status}" != "0" ];then
        rm -rf apr-util-1.6.1.tar.bz2
        curl --connect-timeout 30 -m 360 --fail -LkO https://gitee.com/ElvisTan/software-house/attach_files/974550/download/apr-util-1.6.1.tar.bz2 >>$logs_file 2>&1
        download_status=$?
    fi
    
    if [ "${download_status}" != "0" ];then
        msg="apr-util下载失败，退出安装！！！"
        rm -rf apr-util-1.6.1.tar.bz2
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n35\n$msg\nXXX"
        echo 35
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    
    aprutilPack=`ls -r |grep apr-util|head -n 1`
    aprutiluPack=${aprutilPack%.tar*}
    echo $aprutilPack|grep ".tar.gz">/dev/null && tar zxf $aprutilPack >/dev/null 2>>$logs_file ||tar jxf $aprutilPack >/dev/null 2>>$logs_file
    cd $aprutiluPack && echo -e "XXX\n31\n预编译apr-util中......\nXXX" && echo 31 && ./configure --prefix=/usr/local/apr-util --with-ldap --with-crypto --with-apr=/usr/local/apr/bin/apr-1-config>>/tmp/apache-comp.log 2>>$logs_file&& \
    echo -e "XXX\n32\n编译apr-util中......\nXXX" && echo 32 && make -j8>>/tmp/apache-comp.log 2>>$logs_file && \
    echo -e "XXX\n34\n安装apr-util中......\nXXX" && echo 34 && make install >>/tmp/apache-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装apr-util失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n35\n$msg\nXXX"
        echo 35
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/apache-comp.log
    
    msg="安装pcre"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n40\n$msg\nXXX"
    echo 40
    echo "$msg" >>/tmp/apache-comp.log
    cd $apacheHome
    pcrePack=`ls -r |grep pcre|head -n 1`
    ngPcrePack=`ls -r $workHome/packages/nginx/ 2>/dev/null|grep pcre|head -n 1`
    if [ -z "$pcrePack" ];then
        if [ -z "$ngPcrePack" ];then
           msg="下载PCRE"
           echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
           echo -e "XXX\n42\n$msg\nXXX"
           echo 42
           curl --connect-timeout 30 -m 360 --fail -O https://ftp.pcre.org/pub/pcre/pcre-8.44.tar.bz2 >>$logs_file 2>&1
           download_status=$?
        else
            msg="从Nginx安装目录复制Pcre"
            echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
            echo -e "XXX\n42\n$msg\nXX"
            echo 42
            /bin/cp -f $workHome/packages/nginx/$ngPcrePack . >>$logs_file 2>&1
            download_status=$?
        fi
    fi
    if [ "${download_status}" != "0" ];then
        rm -rf pcre-8.44.tar.bz2
        curl --connect-timeout 30 -m 360 --fail -LkO https://gitee.com/ElvisTan/software-house/attach_files/974552/download/pcre-8.44.tar.bz2 >>$logs_file 2>&1
        download_status=$?
    fi
    
    if [ "${download_status}" != "0" ];then
        msg="pcre下载失败，退出安装！！！"
        rm -rf pcre-8.44.tar.bz2
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n45\n$msg\nXXX"
        echo 45
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    
    pcrePack=`ls -r |grep pcre|head -n 1`
    pcreuPack=${pcrePack%.tar*}
    echo $pcrePack|grep ".tar.gz">/dev/null && tar zxf $pcrePack >/dev/null 2>>$logs_file ||tar jxf $pcrePack >/dev/null 2>>$logs_file
    cd $pcreuPack && echo -e "XXX\n41\n预编译Pcre中......\nXXX" && echo 41 && ./configure --prefix=/usr/local/pcre >>/tmp/apache-comp.log 2>>$logs_file&& \
    echo -e "XXX\n42\n编译Pcre中......\nXXX" && echo 42 && make -j8 >>/tmp/apache-comp.log 2>>$logs_file && \
    echo -e "XXX\n44\n安装Pcre中......\nXXX" && echo 44 && make install >>/tmp/apache-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装pcre失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n45\n$msg\nXXX"
        echo 45
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/apache-comp.log
    
    msg="安装apache"
    rm -rf $apachePath
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n50\n$msg\nXXX"
    echo 50
    echo "$msg" >>/tmp/apache-comp.log
    cd $apacheHome
    tar jxf httpd-${apacheVer}.tar.bz2 >/dev/null 2>>$logs_file
    cd httpd-${apacheVer} && echo -e "XXX\n52\n预编译apache中.....\nXXX" && echo 52 && \
    ./configure --prefix=$apachePath --with-pcre=/usr/local/pcre --with-apr=/usr/local/apr --with-apr-util=/usr/local/apr-util/ --enable-so --enable-dav --enable-ssl --enable-cgi --enable-rewrite --with-zlib --enable-modules=most --enable-mpms-shared=all --with-mpm=event >>/tmp/apache-comp.log 2>>$logs_file && \
    echo -e "XXX\n53\n编译apache中.....\nXXX" && echo 53 && make -j8 >>/tmp/apache-comp.log 2>>$logs_file && \
    echo -e "XXX\n54\n安装apache中.....\nXXX" && echo 54 && make install >>/tmp/apache-comp.log 2>>$logs_file
    if [ "$?" != "0" ];then
        msg="安装apache失败，退出安装！！！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        echo -e "XXX\n55\n$msg\nXXX"
        echo 55
        echo "$msg" >/tmp/.apache.err.info
        sleep 3
        return 1
    fi
    echo " ">>$logs_file
    echo " ">>/tmp/apache-comp.log
    
    msg="创建apache运行用户apache"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n70\n$msg\nXXX"
    cat /etc/passwd|grep "^apache:" >/dev/null
    if [ "$?" != "0" ];then
        useradd -M -s /sbin/nologin apache>>$logs_file 2>&1
    else
        userdel -f apache >/dev/null
    fi
    
    msg="修改apache配置"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n80\n$msg\nXXX"
    echo 80
    sleep 1    
    sed -i "/^#ServerName www.example.com:80/a ServerName localhost:$apachePort" $apachePath/conf/httpd.conf >/dev/null 2>&1
    sed -i 's/^User.*/User apache/g' $apachePath/conf/httpd.conf >/dev/null 2>&1
    sed -i 's/^Group.*/Group apache/g' $apachePath/conf/httpd.conf >/dev/null 2>&1
    sed -i "/AddType application\/x-gzip .gz .tgz/a     AddType application\/x-httpd-php .php" $apachePath/conf/httpd.conf >/dev/null 2>&1
    sed -i "/AddType application\/x-gzip .gz .tgz/a     AddType application\/x-httpd-php-source .phps" $apachePath/conf/httpd.conf >/dev/null 2>&1
    sed -i 's/DirectoryIndex index.html/DirectoryIndex index.html index.php/g' $apachePath/conf/httpd.conf >/dev/null 2>&1
    sed -i "s/^Listen 80/Listen $apachePort/g" $apachePath/conf/httpd.conf >/dev/null 2>&1
    cat >$apachePath/htdocs/phpinfo.php<<'EOF'
<?php
phpinfo();
?>
EOF

    chown -R apache:apache $apachePath >/dev/null
    
    msg="配置apache服务脚本，并启动"
    echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
    echo -e "XXX\n90\n$msg\nXXX"
    echo 90
    sleep 1
    if [ "$system_ver" != "6" ];then
        cat >/etc/systemd/system/httpd.service<<EOF
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
ExecStart=$apachePath/bin/apachectl -k start
ExecReload=$apachePath/bin/apachectl -k graceful
ExecStop=$apachePath/bin/apachectl -k graceful-stop
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF
        chmod 755 /etc/systemd/system/httpd.service >/dev/null 2>&1
        systemctl daemon-reload >/dev/null 2>&1
        systemctl enable httpd >>$logs_file 2>&1 && systemctl start httpd >>$logs_file 2>&1
    else
        cat >/etc/init.d/httpd<<'EOF'
#!/bin/bash
#
# httpd        Startup script for the Apache HTTP Server
#
# chkconfig: - 85 15
# description: The Apache HTTP Server is an efficient and extensible  \
#           server implementing the current HTTP standards.

# Source function library.
. /etc/rc.d/init.d/functions

# Start httpd in the C locale by default.
HTTPD_LANG=${HTTPD_LANG-"C"}

# This will prevent initlog from swallowing up a pass-phrase prompt if
# mod_ssl needs a pass-phrase from the user.
INITLOG_ARGS=""

# Path to the apachectl script, server binary, and short-form for messages.
apachectl=$apachePath/bin/apachectl
httpd=$apachePath/bin/httpd
prog=httpd
pidfile=$apachePath/logs/httpd.pid
lockfile=${LOCKFILE-/var/lock/subsys/httpd}
RETVAL=0
STOP_TIMEOUT=${STOP_TIMEOUT-10}

# The semantics of these two functions differ from the way apachectl does
# things -- attempting to start while running is a failure, and shutdown
# when not running is also a failure.  So we just do it the way init scripts
# are expected to behave here.
start() {
        echo -n $"Starting $prog: "
        LANG=$HTTPD_LANG daemon --pidfile=${pidfile} $httpd $OPTIONS
        RETVAL=$?
        echo
        [ $RETVAL = 0 ] && touch ${lockfile}
        return $RETVAL
}

# When stopping httpd, a delay (of default 10 second) is required
# before SIGKILLing the httpd parent; this gives enough time for the
# httpd parent to SIGKILL any errant children.
stop() {
    status -p ${pidfile} $httpd > /dev/null
    if [[ $? = 0 ]]; then
        echo -n $"Stopping $prog: "
        killproc -p ${pidfile} -d ${STOP_TIMEOUT} $httpd
    else
        echo -n $"Stopping $prog: "
        success
    fi
    RETVAL=$?
    echo
    [ $RETVAL = 0 ] && rm -f ${lockfile} ${pidfile}
}

reload() {
    echo -n $"Reloading $prog: "
    if ! LANG=$HTTPD_LANG $httpd $OPTIONS -t >&/dev/null; then
        RETVAL=6
        echo $"not reloading due to configuration syntax error"
        failure $"not reloading $httpd due to configuration syntax error"
    else
        # Force LSB behaviour from killproc
        LSB=1 killproc -p ${pidfile} $httpd -HUP
        RETVAL=$?
        if [ $RETVAL -eq 7 ]; then
            failure $"httpd shutdown"
        fi
    fi
    echo
}

# See how we were called.
case "$1" in
  start)
    start
    ;;
  stop)
    stop
    ;;
  status)
        status -p ${pidfile} $httpd
    RETVAL=$?
    ;;
  restart)
    stop
    start
    ;;
  condrestart|try-restart)
    if status -p ${pidfile} $httpd >&/dev/null; then
        stop
        start
    fi
    ;;
  force-reload|reload)
        reload
    ;;
  graceful|help|configtest|fullstatus)
    $apachectl $@
    RETVAL=$?
    ;;
  *)
    echo $"Usage: $prog {start|stop|restart|condrestart|try-restart|force-reload|reload|status|fullstatus|graceful|help|configtest}"
    RETVAL=2
esac

exit $RETVAL
EOF
        sed -i "s#\$apachePath#$apachePath#g" /etc/init.d/httpd >/dev/null 2>&1
        chmod 755 /etc/init.d/httpd >/dev/null 2>&1
        chkconfig --add httpd >>$logs_file 2>&1
        chkconfig --level 2345 on httpd >>$logs_file 2>&1
        service httpd start >>$logs_file 2>&1
    fi

    echo 100
    sleep 1   
    } | whiptail --backtitle "Author: Elvis" --title "安装Apache服务" --gauge " " 6 60 0
    if [ -f /tmp/.apache.err.info ];then
        errInfo=`cat /tmp/.apache.err.info`
        rm -rf /tmp/.apache.err.info
        msg="apache安装失败！"
        echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
        whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg\n\n$errInfo" 12 60
        sleep 0.15
        return 1
    fi

    echo "检查apache服务是否运行正常" >>$logs_file
    apachePid=`pidof httpd`
    if [ -z "$apachePid" ];then
        $apachePath/bin/apachectl start >/dev/null 2>&1
        sleep 1
        apachePid=`pidof httpd`
        if [ -z "$apachePid" ];then
          msg="apache启动失败，可能为安装有问题！！！"
          echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
          whiptail --backtitle "Author: Elvis" --title "错误" --msgbox "$msg" 12 60
        else
          msg="apache服务脚本存在问题，无法控制apache服务启停，请检查服务脚本！！！"
          echo "`date "+%Y-%m-%d %H:%M:%S"` $msg" >>$logs_file
          whiptail --backtitle "Author: Elvis" --title "警告" --msgbox "$msg" 12 60
        fi
    else
        apacheVer=`$apachePath/bin/apachectl -v|grep "Server version"|grep -o "[0-9.]"|tr -d "\n"`
        msg="apache $apacheVer安装完成，并且服务运行正常\nNginx安装位置为：$apachePath"
        [[ "$apachePort" = "80" ]] && requestURL="http://$ipaddr" || requestURL="http://$ipaddr:$apachePort"
        echo -e "`date "+%Y-%m-%d %H:%M:%S"` $msg，请访问$requestURL，检验apache是否运行正常。" >>$logs_file
        echo " ">>$logs_file
        whiptail --backtitle "Author: Elvis" --title "信息" --msgbox "$msg\n\
        你可以通过使用service或systemctl命令来控制apache启停\n\n提示：\n请访问$requestURL，检验apache是否运行正常" 12 60
    fi
}